/**
 * Created by TRwin7SP1 on 2016/8/15.
 */
angular.module('inspinia')
  .controller('registerforgetPasswordCtrl',function($scope, $http, toaster, $rootScope) {
    $scope.sendMobile1 = function () {
      if ($scope.username == '') {
        toaster.pop({
          type: 'error',
          title: '请输入用户名',
          showCloseButton: true,
          timeout: 1200
        });
      }
      else if (!(/1\d{10}$/.test($scope.username))) {
        toaster.pop({
          type: 'error',
          title: '请输入正确的手机号码',
          showCloseButton: true,
          timeout: 1200
        });
      }
      else {
        $http.get($rootScope.servers + '/service/user/checkcode?username=' + $scope.username)
          .success(function (data) {
            var result = data.result;
            var num_t1;
            if (result == 0) {
              var num = 60;
              num_t1 = setInterval(function () {
                num--;
                if (num > 0) {
                  $(".code_btn1").html(num + "秒后重新发送");
                  $(".code_btn1").attr("disabled", "disabled");
                } else {
                  clearTimeout(num_t1);
                  $(".code_btn1").html("发送验证码");
                  $(".code_btn1").removeAttr("disabled");
                }
              }, 1000);
            }
            else if (result == -1) {
              var num = 60;
              num_t1 = setInterval(function () {
                num--;
                if (num > 0) {
                  $(".code_btn1").html(num + "秒后重新发送");
                  $(".code_btn1").attr("disabled", "disabled");
                } else {
                  clearTimeout(num_t1);
                  $(".code_btn1").html("发送验证码");
                  $(".code_btn1").removeAttr("disabled");
                }
              }, 1000);

              toaster.pop({
                type: 'error',
                title: '发送失败',
                showCloseButton: true,
                timeout: 600
              });
            }
            else {
              toaster.pop({
                type: 'error',
                title: '服务器连接异常',
                showCloseButton: true,
                timeout: 600
              });
            }
          })
      }
    }
    $scope.resetPassword = function () {
      $http.post($rootScope.servers + '/service/user/findpassword', {
        'username': $scope.username,
        'checkcode': $scope.checkcode
      })
        .success(function (data) {
          var result = data.result;
          if (result == 0) {
            toaster.pop({
              type: 'success',
              title: '重置成功',
              body: '密码重置成功，初始密码为:123456。请点击“返回”，然后登录。',
              showCloseButton: true,
              timeout: 1200
            });
          }
          else if (result == -1) {
            toaster.pop({
              type: 'error',
              title: '密码重置失败',
              // body: '服务器异常',
              showCloseButton: true,
              timeout: 600
            });
          }
          else if (result == 1) {
            toaster.pop({
              type: 'error',
              title: '密码重置失败',
              body: '用户不存在',
              showCloseButton: true,
              timeout: 600
            });
          }
          else {
            toaster.pop({
              type: 'error',
              title: '密码重置失败',
              body: '服务器异常',
              showCloseButton: true,
              timeout: 600
            });
          }
        })
    }
  });
